<?php

function rpc( $json_request ) {
    global $PARAPARA;

    $req_param = $json_request->param;

    if(!$req_param) {
        return new RPC_Error($json_request->operation, "PARAM", "No params");
    };

    $MAX_SCREENNAME_LEN = 50;
    $MAX_SUBJECT_LEN    = 127;
    $MAX_TEXT_LEN       = 10000;

    $topics  = 'std';
    $subject = $req_param->subject;
    if( !is_string( $subject ) || !strlen( $subject ) || 
        ( strlen( $subject ) > $MAX_SUBJECT_LEN ) )
        return new RPC_Error( $json_request->operation, "PARAM", "subject empty or too long" );

    $text = $req_param->text;
    if( !is_string( $text ) || !strlen( $text ) || 
        ( strlen( $text ) > $MAX_TEXT_LEN ) )
        return new RPC_Error( $json_request->operation, "PARAM", "text empty or too long " );


    $account = $PARAPARA->fetch_account();

    #### check if anonymous
    if( ( $account == null) && 
        ( $PARAPARA->config_get('FORUM',  'ALLOW_ANONYMOUS') != 'true') ) {
        return new RPC_Error($json_request->operation, "AUTH", "Not authorized to post anonymously");
    }


    $screen_name = '';
    if($PARAPARA->is_logged_in())
        $screen_name = $PARAPARA->get_screen_name();
    else {
        $screen_name = $req_param->screen_name;
        if( !is_string( $screen_name ) || !strlen( $screen_name ) || 
            ( strlen( $screen_name ) > $MAX_SCREENNAME_LEN ) )
            return new RPC_Error( $json_request->operation, "PARAM", "screen_name empty or too long" );
    }

    $message = new ForumMessage( 0, 0, 0, 
                            $screen_name, 
                            $PARAPARA->get_uid(),
                            $subject,
                            'xxxx-xx-xx 10:10:10',
                            '',
                            ''
        );

    $messagebody = new ForumMessageBody( 0,
                                     $text );

    $message->set_body ( $messagebody );

    $rpc_retval = array( 'status' => 'ok', 
                         'html'   => $message->as_html( array('preview' => 1) )
        );

    return new RPC_Response( $json_request->operation, $rpc_retval );

}

return rpc( $json_request );

?>